package com.lzw.okgoutils.http;

import com.lzw.okgoutils.http.struct.FileBody;
import com.lzw.okgoutils.http.struct.StringFileMap;
import com.lzw.okgoutils.http.struct.StringHashMap;
import com.lzw.okgoutils.http.struct.StringMultiFileMap;
import com.lzw.okgoutils.http.struct.StringObjectMap;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用以封装传递接口提交参数
 * 相同的key对应的value会覆盖
 * @anthor liangfei
 * @e-mail liangfei@hztianque.com
 * @time 2017/8/15
 */
public class RequestParams {
    private StringHashMap mStringParams;
    private StringFileMap mFileParams;//一个key对应一个File
    private StringMultiFileMap mFileListParams;//多个File对应的相同的key
    private String mSaveDir;//下载文件时的保存路径
    private String mSaveName;//下载文件时的保存文件名

    /**
     * 一次性设置所有的字符串参数，不包括文件
     * @param map
     */
    public void setAllStrParams(StringHashMap map){
        if (mStringParams == null) {
            mStringParams = new StringHashMap();
        }
        mStringParams.putAll(map);
    }

    /**
     * 一次性设置所有的参数，包括文件
     * @param map
     */
    public void setAllParams(StringObjectMap map){
        if (mStringParams == null) {
            mStringParams = new StringHashMap();
        }
        if (mFileParams == null) {
            mFileParams = new StringFileMap();
        }
        if (mFileListParams == null) {
            mFileListParams = new StringMultiFileMap();
        }
        if (map == null || map.isEmpty()) {
            return;
        }
        List<String> keyList = map.getKeyList();
        List<Object> valueList = map.getValueList();
        for (int i = 0; i < keyList.size(); i++) {
            String key = keyList.get(i);
            Object value = valueList.get(i);
            if (value instanceof String){
                mStringParams.put(key, (String) value);
            }
            if (value instanceof File){
                mFileParams.put(key, new FileBody((File) value));
            }
            if (value instanceof FileBody){
                mFileParams.put(key, (FileBody) value);
            }
            if (value instanceof List){
                int size = ((List)value).size();
                if(size > 0 && ((List)value).get(0) instanceof File){
                    List<FileBody> flist = new ArrayList<FileBody>();
                    for (int j = 0; j < size; j++){
                        flist.add(new FileBody((File)((List)value).get(j)));
                    }
                    mFileListParams.put(key, flist);
                }
                if(size > 0 && ((List)value).get(0) instanceof FileBody){
                    mFileListParams.put(key, (List<FileBody>) value);
                }
            }
        }
    }

    public void set(String key, String value){
        if (mStringParams == null) {
            mStringParams = new StringHashMap();
        }
        mStringParams.put(key, value);
    }

    /**
     * 设置文件参数， 一个key对应一个文件
     * @param key
     * @param file
     */
    public void set(String key, FileBody file){
        if (mFileParams == null) {
            mFileParams = new StringFileMap();
        }
        mFileParams.put(key, file);
    }

    public void set(String key, File file){
        if (mFileParams == null) {
            mFileParams = new StringFileMap();
        }
        mFileParams.put(key, new FileBody(file));
    }

    /**
     * 设置文件参数， 一个key对应多个文件
     * @param key
     * @param file
     */
    public void setMultiFile(String key, FileBody file){
        if (mFileListParams == null) {
            mFileListParams = new StringMultiFileMap();
        }
        List<FileBody> flist = mFileListParams.get(key);
        if (flist == null) {
            flist = new ArrayList<FileBody>();
            flist.add(file);
            mFileListParams.put(key, flist);
        }else{
            flist.add(file);
        }
    }

    public void setMultiFile(String key, File file){
        if (mFileListParams == null) {
            mFileListParams = new StringMultiFileMap();
        }
        List<FileBody> flist = mFileListParams.get(key);
        if (flist == null) {
            flist = new ArrayList<FileBody>();
            flist.add(new FileBody(file));
            mFileListParams.put(key, flist);
        }else{
            flist.add(new FileBody(file));
        }
    }

    public void setMultiFile(String key, List<FileBody> fileList){
        if (mFileListParams == null) {
            mFileListParams = new StringMultiFileMap();
        }
        if (fileList == null || fileList.size() == 0) {
            return;
        }
        mFileListParams.put(key, fileList);
    }

    public void setMultiFile2(String key, List<File> fileList){
        if (mFileListParams == null) {
            mFileListParams = new StringMultiFileMap();
        }
        if (fileList == null || fileList.size() == 0) {
            return;
        }
        List<FileBody> flist = new ArrayList<FileBody>();
        for (File file : fileList) {
            flist.add(new FileBody(file));
        }
        mFileListParams.put(key, flist);
    }

    public void setSaveDir(String dir){
        mSaveDir = dir;
    }

    public void setSaveName(String fname){
        mSaveName = fname;
    }

    /**
     * 返回所有参数值, 以<String, String>的形式, 不包括File
     * @return StringHashMap
     */
    public StringHashMap getAllParamStrs(){
        StringHashMap map = new StringHashMap();
        if (mStringParams != null) {
            map.putAll(mStringParams);
        }
        return map;
    }

    /**
     * 返回所有参数值, 以<String, String>的形式, 包括File路径(但不是多个File对应同一key的)
     * @return StringHashMap
     */
    public StringHashMap getAllParamStrWithFile(){
        StringHashMap map = getAllParamStrs();
        if (mFileParams != null) {
            for (Map.Entry<String, FileBody> entry : mFileParams.entrySet()){
                map.put(entry.getKey(), String.valueOf(entry.getValue()));
            }
        }
        return map;
    }

    /**
     * 获取所有的文件, 一个文件对应一个key的情况
     * @return HashMap<String, FileBody>
     */
    public StringFileMap getFileParams(){
        return mFileParams;
    }

    /**
     * 获取所有的文件, 多个文件对应一个key的情况
     * @return HashMap<String, List<FileBody>>
     */
    public StringMultiFileMap getMultiFileParams(){
        return mFileListParams;
    }

    public String getSaveDir(){
        return mSaveDir;
    }

    public String getSaveName(){
        return mSaveName;
    }

    /**
     * 一次性将params中的所有参数保存到当前实例当中，同名会覆盖
     * @param params
     */
    public void setAllParams(RequestParams params){
        if (params == null) {
            return;
        }
        if (params.getInternalStringMap() != null) {
            if (mStringParams == null) {
                mStringParams = new StringHashMap();
            }
            mStringParams.putAll(params.getInternalStringMap());
        }
        if (params.getFileParams() != null) {
            if (mFileParams == null) {
                mFileParams = new StringFileMap();
            }
            mFileParams.putAll(params.getFileParams());
        }
        if (params.getMultiFileParams() != null) {
            if (mFileListParams == null) {
                mFileListParams = new StringMultiFileMap();
            }
            mFileListParams.putAll(params.getMultiFileParams());
        }
    }

    public StringHashMap getInternalStringMap(){
        return mStringParams;
    }

    public String getStringValue(String key){
        if (mStringParams != null) {
            return mStringParams.get(key);
        }
        return null;
    }

    public FileBody getFileBody(String key){
        if (mFileParams != null) {
            return mFileParams.get(key);
        }
        return null;
    }

    public List<FileBody> getMultiFileBody(String key){
        if (mFileListParams != null) {
            return mFileListParams.get(key);
        }
        return null;
    }

    public Object get(String key){
        if (mStringParams != null && mStringParams.containsKey(key)) {
            return mStringParams.get(key);
        }
        if (mFileParams != null && mFileParams.containsKey(key)) {
            return mFileParams.get(key);
        }
        if (mFileListParams != null && mFileListParams.containsKey(key)) {
            return mFileListParams.get(key);
        }
        return null;
    }

}
